package com.mls.fivedistrict.controller.portal;


import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.NewsShareReport;
import com.mls.fivedistrict.server.NewsService;
import com.mls.fivedistrict.server.NewsShareReportService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 新闻分享报表 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-12
 */
@RestController
@RequestMapping("/api/app")
public class NewsShareReportController extends BaseController {
    @Autowired
    NewsShareReportService newsShareReportService;
    @Autowired
    NewsService newsService;

    /**
     * 新闻分享接口
     *
     * @param newsShareReport
     * @return
     */
    @PostMapping("/v1/newsShareReport")
    public ServiceResponse addNewsShareReport(@RequestBody NewsShareReport newsShareReport) {
        BeanValidator.check(newsShareReport);
        newsShareReport.setUserId(getUser().getId());
        Long newsId = newsShareReport.getNewsId();
        boolean update = newsService.addShareTimes(newsId);
        boolean insert = newsShareReportService.addNewsShareReport(newsShareReport);
        boolean flag = insert && update;
        if (update) {
            return ServiceResponse.createSuccessResponse("分享新闻成功!");
        } else {
            //分享失败
            return ServiceResponse.createErrorResponse("分享失败!");
        }
    }
}

